const initialState = {
  comments: {
    end_id: '',
    last_id: '',
    results: [] as CommentType[],
  },
  originComment: {} as CommentType,

  // 1. API可复用
  // 2. Action保存评论的回复了列表、reducer中声明状态
  /** 保存的评论的回复信息 */
  replyComments: {
    end_id: '',
    last_id: '',
    results: [] as CommentType[],
  },
  // 3. 扩展actionType
  // 4.编写保存逻辑
  // 5. 触发
};

export default function commentReducer(
  state = initialState,
  action: CommentActionType
): typeof initialState {
  switch (action.type) {
    case 'comment/saveComments':
      return {
        ...state,
        comments: action.payload,
      };
    case 'comment/clearComments':
      return {
        ...state,
        comments: {} as any,
      };
    case 'comment/saveOriginComment':
      return {
        ...state,
        originComment: action.payload,
      };
    case 'comment/clearOriginComment':
      return {
        ...state,
        originComment: {} as CommentType,
      };

    case 'comment/saveReplyComments':
      return {
        ...state,
        replyComments: action.payload,
      };
    default:
      return state;
  }
}

export type CommentType = {
  com_id: string;
  content: string;
  reply_count: number;
  pubdate: string;
  is_followed: boolean;
  is_liking: boolean;
  like_count: number;
  aut_id: string;
  aut_name: string;
  aut_photo: string;
};
export type CommentActionType =
  | {
      type: 'comment/saveComments';
      payload: typeof initialState.comments;
    }
  | { type: 'comment/clearComments' }
  | { type: 'comment/saveOriginComment'; payload: CommentType }
  | { type: 'comment/clearOriginComment' }
  | { type: 'comment/saveReplyComments'; payload: typeof initialState.replyComments };
